package com.lego.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lego.service.OrdersService;
import com.lego.service.impl.OrdersServiceImpl;

@WebServlet("/changeStaById.do")
public class ChangeStatusByorderIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8"); // 设置返回 JSON
        
        OrdersService os = new OrdersServiceImpl();
        int orderId = 0;
        try {
            orderId = Integer.parseInt(request.getParameter("orderId"));
        } catch (Exception e) {
            System.out.println("没有接收到orderId");
            response.getWriter().write("{\"code\":400,\"message\":\"缺少orderId参数\"}");
            return;
        }
        
        String orderStatus = request.getParameter("orderStatus");
        int row = 0;
        if(orderStatus.equals("shipped")) {
        	row = os.changeStatusById(orderStatus, orderId);
        }else {
        	row = os.changeStatusTime(orderStatus, orderId);
        }
        
        if (row == 1) {
            response.getWriter().write("{\"code\":200,\"message\":\"状态更新成功\"}");
        } else {
            response.getWriter().write("{\"code\":500,\"message\":\"修改状态失败\"}");
        }
    }
}
